package com.hymjweb.demo.hymj.master_bill_manage.pull.controller;

import com.alibaba.fastjson.JSONObject;
import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.framework.util.DateUtil;
import com.hymjweb.demo.hymj.master_bill_manage.auto.bean.BMasterBillAutoPojo;
import com.hymjweb.demo.hymj.master_bill_manage.auto.service.BMasterBillAutoService;
import com.hymjweb.demo.hymj.master_bill_manage.flight.bean.BMasterBillFlightPojo;
import com.hymjweb.demo.hymj.master_bill_manage.flight.service.BMasterBillFlightService;
import com.hymjweb.demo.hymj.master_bill_manage.load.bean.BNativeBillLoadPojo;
import com.hymjweb.demo.hymj.master_bill_manage.load.service.BNativeBillLoadService;
import com.hymjweb.demo.hymj.master_bill_manage.pull.bean.BMasterBillPullPojo;
import com.hymjweb.demo.hymj.master_bill_manage.pull.service.BMasterBillPullService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.hymjweb.demo.hymj.master_bill_manage.pull.controller
 * @Author: luoge
 * @CreateTime: 2020-12-30 16:39
 * @Description:
 */

@Controller
@RequestMapping("/pages/master_bill_manage/pull")
public class BMasterBillPullController extends BaseController {

    @Autowired
    private BMasterBillPullService bMasterBillPullService;

    @Autowired
    private BMasterBillFlightService bMasterBillFlightService;

    @Autowired
    private BMasterBillAutoService bMasterBillAutoService;

    @Autowired
    private BNativeBillLoadService bNativeBillLoadService;

    @RequestMapping("/showPullList")
    public String showPullList() {
        return "pages/master_bill_manage/pull/masterBillPullList";
    }


    @RequestMapping("/showPullAdd")
    public String showPullAdd(String masterBillId, Long masterBillTypeId) throws Exception {

        if (DataTypeUtil.isEmptyStr(masterBillId) || masterBillTypeId == null) {
            throwException("拉货主单加载参数错误!");
        }
        setRequestAttribute("masterBillId", masterBillId);
        setRequestAttribute("masterBillTypeId", masterBillTypeId);
        if (masterBillTypeId.longValue() == SysConstant.XT_DIC_MASTER_BILL_TYPE.FLIGHT.getId()) {
            BMasterBillFlightPojo masterBillFlightPojo = bMasterBillFlightService.getBMasterBillFlightPojoById(masterBillId);
            masterBillFlightPojo.setComment("");
            setRequestAttribute("masterBillPojo", objectToJSONObject(masterBillFlightPojo));
        } else {
            BMasterBillAutoPojo masterBillAutoPojo = bMasterBillAutoService.getBMasterBillAutoPojoById(masterBillId);
            masterBillAutoPojo.setComment("");
            setRequestAttribute("masterBillPojo", objectToJSONObject(masterBillAutoPojo));
        }

        // 获取对应的配载列表
        Map<String, Object> param = new HashMap<>();
        param.put("masterBillId", masterBillId);
        param.put("masterBillTypeId", masterBillTypeId);
        List<BNativeBillLoadPojo> loadPojoList = bNativeBillLoadService.queryNativeBillListNoPage(param);

        if(loadPojoList==null || loadPojoList.isEmpty()){
            throwException("配载列表加载失败!");
        }

        for (BNativeBillLoadPojo loadPojo : loadPojoList) {
            loadPojo.setShareTotalCostComment("");
        }

        setRequestAttribute("loadPojoList", objectToJSONObject(loadPojoList));

        return "pages/master_bill_manage/pull/pullAdd";
    }

    @RequestMapping("/queryMasterBillPull")
    @ResponseBody
    public Object queryMasterBillPull(@RequestParam(required = true) Map param) throws Exception {
        return bMasterBillPullService.getPageList(param);
    }

    @RequestMapping("/addPullData")
    @ResponseBody
    public Object addPullData(@RequestBody(required = true) JSONObject pullJsonObject) throws Exception {
        if(pullJsonObject==null || pullJsonObject.isEmpty()){
            throwException("添加拉货信息参数错误!");
        }

        // 1.jsonObject 转 BMasterBillPullPojo并填充相关缺失参数
        BMasterBillPullPojo bMasterBillPullPojo = bMasterBillPullService.jsonObjConvertToPojo(pullJsonObject);

        if(bMasterBillPullPojo==null){
            throwException("拉货信息对象转换失败!");
        }

        // 2.拉货对象入库处理，同步处理相关事务(拉货后的分单成本核算)
        int effectRows = bMasterBillPullService.pullIntoDb(bMasterBillPullPojo);

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("添加拉货数据成功!");

        if (effectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("添加拉货数据失败!");
        }

        return baseResultMap;
    }

    @RequestMapping("/updatePullData")
    @ResponseBody
    public Object updatePullData(@RequestBody(required = true) JSONObject pullJsonObject) throws Exception {
        if(pullJsonObject==null || pullJsonObject.isEmpty()){
            throwException("更新主单拉货信息参数错误!");
        }

        // 1.jsonObject 转 BMasterBillPullPojo并填充相关缺失参数
        BMasterBillPullPojo bMasterBillPullPojo = pullJsonObject.toJavaObject(BMasterBillPullPojo.class);
        if(bMasterBillPullPojo==null){
            throwException("拉货信息对象转换失败!");
        }

        bMasterBillPullPojo.setUpdateTime(DateUtil.getSysDate());
        bMasterBillPullPojo.setUpdateUserId(this.getLoginUserId());



        // 2.拉货对象入库处理，同步处理相关事务(拉货后的分单成本核算)
        int effectRows = bMasterBillPullService.updatePullIntoDb(bMasterBillPullPojo);

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("更新主单拉货数据成功!");

        if (effectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("更新主单拉货数据失败!");
        }

        return baseResultMap;
    }

}
